package main

import (
	"errors"
	"fmt"
	"imooc-product/encrypt"
	filter2 "imooc-product/filter"
	"net/http"
)

func Check(w http.ResponseWriter,r *http.Request)  {
	//执行正常的业务逻辑
	fmt.Println("执行check")
}
//统一验证拦截器，每个接口都需要提前验证
func Auth(w http.ResponseWriter,r *http.Request)error  {
	fmt.Println("执行验证")
	//添加基于cookie的权限验证
	err := CheckUserInfo(r)
	if err != nil{
		return err
	}
	return nil
}
func CheckUserInfo(r *http.Request)error  {
	//获取Uid,cookie
	uidCookie ,err := r.Cookie("uid")
	if err != nil{
		return errors.New("用户uid Cookie获取失败")
	}
	//获取用户加密串
	signCookie,err := r.Cookie("sign")
	if err != nil{
		return errors.New("用户加密串 cookie获取失败")
	}
	//对信息进行解密
	signByte,err := encrypt.DePwsCode(signCookie.Value)
	if err != nil{
		return errors.New("加密串已被篡改")
	}
	if checkInfo(uidCookie.Value,string(signByte)){
		return nil
	}
	return errors.New("身份校验失败")
}

//自定义逻辑判断
func checkInfo(checkStr string,signStr string)bool  {
	if checkStr == signStr{
		return true
	}
	return false
}
func main()  {
	//1、过滤器
	filter := filter2.NewFilter()
	//注册拦截器
	filter.RegisterFilterUri("/check",Auth)
	http.HandleFunc("/check",filter.Handle(Check))
	//2、启动服务
	http.ListenAndServe(":8083",nil)
}
